# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms.dm.dm_whole_decide_duty_main_pres_dt import jms_dm__dm_whole_decide_duty_main_pres_dt

label = f"dm_whole_decide_duty_main_pres_dt_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dm__dm_whole_decide_duty_main_pres_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_whole_decide_duty_main_pres_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_whole_decide_duty_main_pres_dt PARTITION (
         p{{{{ execution_date | date_add(-0) | cst_ds_nodash }}}} 
        ,p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-3) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-4) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-5) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-6) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-7) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-8) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-9) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-10) | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-0) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-3) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-4) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-5) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-6) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-7) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-8) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-9) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_whole_decide_duty_main_pres_dt/dt={{{{ execution_date | date_add(-10) | cst_ds }}}}/*"
        )
        INTO TABLE dm_whole_decide_duty_main_pres_dt
        FORMAT AS 'PARQUET'(
            ziyou_platform_effect
           ,cainiao_platform_effect
           ,order_source_code
           ,order_source_name
           ,duty_type
           ,duty_main_code
           ,duty_main_name
           ,duty_main_type
           ,duty_main_fran_code
           ,duty_main_fran_name
           ,duty_main_agent_code
           ,duty_main_agent_name
           ,duty_provider_code
           ,duty_provider_desc
           ,ziyou_duty_type
           ,ziyou_duty_main_code
           ,ziyou_duty_main_name
           ,ziyou_duty_main_type
           ,ziyou_duty_main_fran_code
           ,ziyou_duty_main_fran_name
           ,ziyou_duty_main_agent_code
           ,ziyou_duty_main_agent_name
           ,ziyou_duty_provider_code
           ,ziyou_duty_provider_desc
           ,start_network_code
           ,start_network_name
           ,start_city_code
           ,start_city_name
           ,start_provider_code
           ,start_provider_name
           ,start_fran_code
           ,start_fran_name
           ,start_agent_code
           ,start_agent_name
           ,start_center_code
           ,start_center_name
           ,end_network_code
           ,end_network_name
           ,end_city_code
           ,end_city_name
           ,end_provider_code
           ,end_provider_name
           ,end_fran_code
           ,end_fran_name
           ,end_agent_code
           ,end_agent_name
           ,end_center_code
           ,end_center_name
           ,jingtai_lyqs_cnt
           ,dizhi_jxcw_cnt
           ,jingtai_my_cnt
           ,jiedanhou_ls_cnt
           ,jiaojian_jbcw_cnt
           ,cuojiao_cnt
           ,jiaojian_bzd_cnt
           ,zhongzhuan_jbcw_cnt
           ,cuofa_cnt
           ,fengche_wd_cnt
           ,caozuo_yw_cnt
           ,fache_wd_cnt
           ,yunshu_wd_cnt
           ,jingang_zybjs_cnt
           ,zhongzhuan_cf_cnt
           ,sanhuo_bjs_cnt
           ,paijian_cf_cnt
           ,qianshou_wd_cnt
           ,tuizhuan_cnt
           ,duty_shipment_no
           ,duty_cnt
           ,zy_dizhi_jxcw_cnt
           ,zy_jiedanhou_ls_cnt
           ,zy_jiaojian_jbcw_cnt
           ,zy_cuojiao_cnt
           ,zy_jiaojian_bzd_cnt
           ,zy_zhongzhuan_jbcw_cnt
           ,zy_cuofa_cnt
           ,zy_fengche_wd_cnt
           ,zy_caozuo_yw_cnt
           ,zy_fache_wd_cnt
           ,zy_yunshu_wd_cnt
           ,zy_jingang_zybjs_cnt
           ,zy_zhongzhuan_cf_cnt
           ,zy_sanhuo_bjs_cnt
           ,zy_paijian_cf_cnt
           ,zy_qianshou_wd_cnt
           ,zy_tuizhuan_cnt
           ,ziyou_duty_shipment_no
           ,zy_duty_cnt
           ,cainiao_plan_sign_date
           ,ziyou_plan_sign_date
           ,aging_sign_date
           ,actual_sign_date
           ,customer_code
           ,customer_name
           ,duty_main_bagg_user_code
           ,duty_main_bagg_user_name
           ,duty_main_pre_site_code
           ,duty_main_pre_site_name
           ,duty_main_pre_site_type
           ,duty_main_next_site_code
           ,duty_main_next_site_name
           ,duty_main_next_site_type
           ,ziyou_duty_main_bagg_user_code
           ,ziyou_duty_main_bagg_user_name
           ,ziyou_duty_main_pre_site_code
           ,ziyou_duty_main_pre_site_name
           ,ziyou_duty_main_pre_site_type
           ,ziyou_duty_main_next_site_code
           ,ziyou_duty_main_next_site_name
           ,ziyou_duty_main_next_site_type
           ,jingtai_my_dbc_cnt         
           ,xiangzhen_js_cnt           
           ,zhongzhuan_lsdd_cnt        
           ,zy_zhongzhuan_lsdd_cnt     
           ,duty_city_code             
           ,duty_city_name             
           ,ziyou_duty_city_code       
           ,ziyou_duty_city_name    
           ,beiyongly_cnt        
           ,zhuanyou_cnt         
           ,zy_beiyongly_cnt     
           ,zy_zhuanyou_cnt      
        )
        columns from path as (dt)
        set(
            ziyou_platform_effect             =  ziyou_platform_effect
           ,cainiao_platform_effect           =  cainiao_platform_effect
           ,order_source_code                 =  order_source_code
           ,order_source_name                 =  order_source_name
           ,duty_type                         =  duty_type
           ,duty_main_code                    =  duty_main_code
           ,duty_main_name                    =  duty_main_name
           ,duty_main_type                    =  duty_main_type
           ,duty_main_fran_code               =  duty_main_fran_code
           ,duty_main_fran_name               =  duty_main_fran_name
           ,duty_main_agent_code              =  duty_main_agent_code
           ,duty_main_agent_name              =  duty_main_agent_name
           ,duty_provider_code                =  duty_provider_code
           ,duty_provider_desc                =  duty_provider_desc
           ,ziyou_duty_type                   =  ziyou_duty_type
           ,ziyou_duty_main_code              =  ziyou_duty_main_code
           ,ziyou_duty_main_name              =  ziyou_duty_main_name
           ,ziyou_duty_main_type              =  ziyou_duty_main_type
           ,ziyou_duty_main_fran_code         =  ziyou_duty_main_fran_code
           ,ziyou_duty_main_fran_name         =  ziyou_duty_main_fran_name
           ,ziyou_duty_main_agent_code        =  ziyou_duty_main_agent_code
           ,ziyou_duty_main_agent_name        =  ziyou_duty_main_agent_name
           ,ziyou_duty_provider_code          =  ziyou_duty_provider_code
           ,ziyou_duty_provider_desc          =  ziyou_duty_provider_desc
           ,start_network_code                =  start_network_code
           ,start_network_name                =  start_network_name
           ,start_city_code                   =  start_city_code
           ,start_city_name                   =  start_city_name
           ,start_provider_code               =  start_provider_code
           ,start_provider_name               =  start_provider_name
           ,start_fran_code                   =  start_fran_code
           ,start_fran_name                   =  start_fran_name
           ,start_agent_code                  =  start_agent_code
           ,start_agent_name                  =  start_agent_name
           ,start_center_code                 =  start_center_code
           ,start_center_name                 =  start_center_name
           ,end_network_code                  =  end_network_code
           ,end_network_name                  =  end_network_name
           ,end_city_code                     =  end_city_code
           ,end_city_name                     =  end_city_name
           ,end_provider_code                 =  end_provider_code
           ,end_provider_name                 =  end_provider_name
           ,end_fran_code                     =  end_fran_code
           ,end_fran_name                     =  end_fran_name
           ,end_agent_code                    =  end_agent_code
           ,end_agent_name                    =  end_agent_name
           ,end_center_code                   =  end_center_code
           ,end_center_name                   =  end_center_name
           ,jingtai_lyqs_cnt                  =  jingtai_lyqs_cnt
           ,dizhi_jxcw_cnt                    =  dizhi_jxcw_cnt
           ,jingtai_my_cnt                    =  jingtai_my_cnt
           ,jiedanhou_ls_cnt                  =  jiedanhou_ls_cnt
           ,jiaojian_jbcw_cnt                 =  jiaojian_jbcw_cnt
           ,cuojiao_cnt                       =  cuojiao_cnt
           ,jiaojian_bzd_cnt                  =  jiaojian_bzd_cnt
           ,zhongzhuan_jbcw_cnt               =  zhongzhuan_jbcw_cnt
           ,cuofa_cnt                         =  cuofa_cnt
           ,fengche_wd_cnt                    =  fengche_wd_cnt
           ,caozuo_yw_cnt                     =  caozuo_yw_cnt
           ,fache_wd_cnt                      =  fache_wd_cnt
           ,yunshu_wd_cnt                     =  yunshu_wd_cnt
           ,jingang_zybjs_cnt                 =  jingang_zybjs_cnt
           ,zhongzhuan_cf_cnt                 =  zhongzhuan_cf_cnt
           ,sanhuo_bjs_cnt                    =  sanhuo_bjs_cnt
           ,paijian_cf_cnt                    =  paijian_cf_cnt
           ,qianshou_wd_cnt                   =  qianshou_wd_cnt
           ,tuizhuan_cnt                      =  tuizhuan_cnt
           ,duty_shipment_no                  =  duty_shipment_no
           ,duty_cnt                          =  duty_cnt
           ,zy_dizhi_jxcw_cnt                 =  zy_dizhi_jxcw_cnt
           ,zy_jiedanhou_ls_cnt               =  zy_jiedanhou_ls_cnt
           ,zy_jiaojian_jbcw_cnt              =  zy_jiaojian_jbcw_cnt
           ,zy_cuojiao_cnt                    =  zy_cuojiao_cnt
           ,zy_jiaojian_bzd_cnt               =  zy_jiaojian_bzd_cnt
           ,zy_zhongzhuan_jbcw_cnt            =  zy_zhongzhuan_jbcw_cnt
           ,zy_cuofa_cnt                      =  zy_cuofa_cnt
           ,zy_fengche_wd_cnt                 =  zy_fengche_wd_cnt
           ,zy_caozuo_yw_cnt                  =  zy_caozuo_yw_cnt
           ,zy_fache_wd_cnt                   =  zy_fache_wd_cnt
           ,zy_yunshu_wd_cnt                  =  zy_yunshu_wd_cnt
           ,zy_jingang_zybjs_cnt              =  zy_jingang_zybjs_cnt
           ,zy_zhongzhuan_cf_cnt              =  zy_zhongzhuan_cf_cnt
           ,zy_sanhuo_bjs_cnt                 =  zy_sanhuo_bjs_cnt
           ,zy_paijian_cf_cnt                 =  zy_paijian_cf_cnt
           ,zy_qianshou_wd_cnt                =  zy_qianshou_wd_cnt
           ,zy_tuizhuan_cnt                   =  zy_tuizhuan_cnt
           ,ziyou_duty_shipment_no            =  ziyou_duty_shipment_no
           ,zy_duty_cnt                       =  zy_duty_cnt
           ,cainiao_plan_sign_date            =  cainiao_plan_sign_date
           ,ziyou_plan_sign_date              =  ziyou_plan_sign_date
           ,aging_sign_date                   =  aging_sign_date
           ,actual_sign_date                  =  actual_sign_date
           ,customer_code                     =  customer_code
           ,customer_name                     =  customer_name
           ,duty_main_bagg_user_code          =  duty_main_bagg_user_code
           ,duty_main_bagg_user_name          =  duty_main_bagg_user_name
           ,duty_main_pre_site_code           =  duty_main_pre_site_code
           ,duty_main_pre_site_name           =  duty_main_pre_site_name
           ,duty_main_pre_site_type           =  duty_main_pre_site_type
           ,duty_main_next_site_code          =  duty_main_next_site_code
           ,duty_main_next_site_name          =  duty_main_next_site_name
           ,duty_main_next_site_type          =  duty_main_next_site_type
           ,ziyou_duty_main_bagg_user_code    =  ziyou_duty_main_bagg_user_code
           ,ziyou_duty_main_bagg_user_name    =  ziyou_duty_main_bagg_user_name
           ,ziyou_duty_main_pre_site_code     =  ziyou_duty_main_pre_site_code
           ,ziyou_duty_main_pre_site_name     =  ziyou_duty_main_pre_site_name
           ,ziyou_duty_main_pre_site_type     =  ziyou_duty_main_pre_site_type
           ,ziyou_duty_main_next_site_code    =  ziyou_duty_main_next_site_code
           ,ziyou_duty_main_next_site_name    =  ziyou_duty_main_next_site_name
           ,ziyou_duty_main_next_site_type    =  ziyou_duty_main_next_site_type
           ,jingtai_my_dbc_cnt                =  jingtai_my_dbc_cnt    
           ,xiangzhen_js_cnt                  =  xiangzhen_js_cnt      
           ,zhongzhuan_lsdd_cnt               =  zhongzhuan_lsdd_cnt   
           ,zy_zhongzhuan_lsdd_cnt            =  zy_zhongzhuan_lsdd_cnt
           ,duty_city_code                    =  duty_city_code        
           ,duty_city_name                    =  duty_city_name        
           ,ziyou_duty_city_code              =  ziyou_duty_city_code  
           ,ziyou_duty_city_name              =  ziyou_duty_city_name  
           ,beiyongly_cnt                     =  beiyongly_cnt
           ,zhuanyou_cnt                      =  zhuanyou_cnt
           ,zy_beiyongly_cnt                  =  zy_beiyongly_cnt
           ,zy_zhuanyou_cnt                   =  zy_zhuanyou_cnt
           ,dt                                =  dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

doris_jms_dm__dm_whole_decide_duty_main_pres_dt << jms_dm__dm_whole_decide_duty_main_pres_dt
